const fs = require('fs')//引入操作文件的模块
const querystring = require('querystring');//引入查询字符串模块
let value = fs.readFileSync('persons.json').toString()//读取persons.json文件转成字符串
value = JSON.parse(value)//将字符串转成对象方便操作


function login(user1) {

    let user = user1//给参数赋值
    // 登录
    let admin = value.find(item => user.username === item.username && user.password === item.password)//箭头函数的参数为true
    if (admin) {
        return '登录成功'

    } else {
        return '用户名或密码错误'

    }
}
function register(user1) {
    // let user = { username: "admin1", password: "12345" }
    // let user = {"username":"xm3","password":"333"}
    let user = user1
    // 注册
    let tag = value.find(item => item.username === user.username)
    if (tag) {
        // console.log('该用户已注册');
        return '该用户已注册'
        //  document.write('该用户已注册')
    } else {

        value.push(user)//将user的值加进去
        fs.writeFileSync('persons.json', JSON.stringify(value), { flag: 'w' })//将对象转成字符串以写的形式写进去
        // console.log(value);
        return '注册成功'
    }
}
module.exports = {
    login,
    register
}//暴露login, register
   